package com.sfac.springCloudTest.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.sfac.springCloudTest.service.CityService;
import org.sfac.common.vo.Result;
import org.sfac.common.vo.Search;
import org.sfac.test.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     * 127.0.0.1/api/city----post
     * {"cityName":"chengdu","localCityName":"成都","countryId":522}
     */
    @PostMapping(value = "/city",consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city) {

        return cityService.insertCity(city);
    }

    /**
     * 127.0.0.1/api/city----put
     * cityId:2259,cityName:hhh1,localCityName:黄海华1
     *
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }

    /**
     * 127.0.0.1/api/city/2259----delete
     *
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId){
        return cityService.deleteCityByCityId(cityId);
    }


    /**
     * 127.0.0.1/api/city/2259----get
     *
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        //int i = 3/0;
        return cityService.getCityByCityId(cityId);
    }


    /**
     *
     *127.0.0.1/api/city?cityName=  &  localCityName=  ----get
     */
    @GetMapping("/city")
    public City getCityByNames(@RequestParam String cityName,
                               @RequestParam String localCityName) {
        return cityService.getCityByNames(cityName,localCityName);
    }


    /**
     *
     *127.0.0.1/api/cities/522 ----get
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     *
     *127.0.0.1/api/cities ----post
     * {"currentPage":1,"pageSize":5,"sort":"city_name","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/cities",consumes = "application/json")
    public PageInfo<City> getCitiesBySearch(@RequestBody Search search) {
        return cityService.getCitiesBySearch(search);
    }

    /**
     *127.0.0.1/api/cities/v2 ----post
     * {"currentPage":1,"pageSize":5,"sort":"city_name","direction":"desc","keyword":""}
     */
    /*@PostMapping(value = "/cities/v2",consumes = "application/json")
    public Page<City> getCitiesBySearch2(@RequestBody Search search){

        //return cityService.getCitiesBySearch2(search);
    }*/
}
/*
    * 4种方式：                 处理方式：               请求方式:（根据情况而定）
    * 1-接受前端的  json数据      @RequestBody
    * 2-接受前端的  form表单数据   @ModelAttribute
    * 3-接受前端的  path数据      @PathVariable
    * 4-接受前端的  属性参数数据    @RequestParam
* */